//
//  ZLCalendarSaleDetailHeaderView.swift
//  TestDemo
//
//  Created by ayin on 2019/5/29.
//  Copyright © 2019 aYin. All rights reserved.
//  日历-发售详情-头部商品信息view

import UIKit

protocol ZLCalendarSaleDetailHeaderViewDelegate : NSObjectProtocol {
    //MARK: 声明代理
    /// 商品信息点击事件代理
    ///
    /// - Parameter dataModel: 数据
    func productInfoViewTapAction(dataModel: Any)
    
    /// 收藏事件代理
    ///
    /// - Parameter dataModel: 数据
    func collectionAction(sender: UIButton, dataModel: Any)
    
    /// 商品图片浏览事件代理
    ///
    /// - Parameters:
    ///   - imgView: 当前图片本身
    ///   - index: 图片索引
    func subProductImgViewTapAction(imgView: UIImageView, index: Int)
}

extension ZLCalendarSaleDetailHeaderViewDelegate {
    func productInfoViewTapAction(dataModel: Any) {}
    func collectionAction(sender: UIButton, dataModel: Any) {}
    func subProductImgViewTapAction(imgView: UIImageView, index: Int) {}
}

class ZLCalendarSaleDetailHeaderView: UIView {
    //MARK: - 声明属性
    //背景view
    lazy var bgImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.isUserInteractionEnabled = true
        imgView.image = UIImage(named: "calendar_detailBg_red")
        imgView.cornerRadius(4)
        return imgView
    }()
    
    //商品信息view
    lazy var productInfoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    //封面图片
    lazy var coverImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.backgroundColor = UIColor.blue
        return imgView
    }()
    
    //标题
    lazy var titleLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangSemibold(size: 13.up), text: nil, textColor: UIColor.fc1, numberLine: 2, alignment: .left)
        return label
    }()
    
    //价格
    lazy var priceLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.dinot(size: 18.up), text: nil, textColor: UIColor.fc11, numberLine: 1, alignment: .left)
        return label
    }()
    
    //货号
    lazy var numberLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 9.up), text: nil, textColor: UIColor.fc2, numberLine: 1, alignment: .left)
        return label
    }()
    
    //商品其他图片
    lazy var subProductImgsView: UIView = {
        let view = UIView()
        return view
    }()
    
    //收藏
    lazy var collectionBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 9.up), title: "收藏", titleColor: UIColor.fc2, image: "calendar_collection_unselected", bgColor: UIColor.yellow)
//        btn.layoutButton(with: .imageTop, imageTitlespace: 3.0)
//        btn.setEnlargeEdgeWithTop(10, right: 10, bottom: 10, left: 10)
        btn.setImage(UIImage(named: "calendar_collection_unselected"), for: .normal)
        btn.setImage(UIImage(named: "calendar_collection_selected"), for: .selected)
        btn.addTarget(self, action: #selector(collectionBtnAction(sender:)), for: .touchUpInside)
        return btn
    }()
    
    let hSpace: CGFloat = 15
    let vSpace: CGFloat = 25
    weak var delegate: ZLCalendarSaleDetailHeaderViewDelegate?
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 搭建UI
    func setupUI() {
        self.addSubview(bgImgView)
        bgImgView.addSubview(productInfoView)
        productInfoView.addSubview(coverImgView)
        productInfoView.addSubview(titleLB)
        productInfoView.addSubview(priceLB)
        productInfoView.addSubview(numberLB)
        productInfoView.addSubview(subProductImgsView)
        productInfoView.addSubview(collectionBtn)
        
        bgImgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        productInfoView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(hSpace)
            make.top.equalToSuperview().offset(statusBarAndNavigationBarHeight)
            make.right.equalToSuperview().offset(-hSpace)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        coverImgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(hSpace)
            make.top.equalToSuperview().offset(vSpace)
            make.width.height.equalTo(84.up)
        }
        
        titleLB.snp.makeConstraints { (make) in
            make.left.equalTo(coverImgView.snp.right).offset(10)
            make.top.equalToSuperview().offset(vSpace)
            make.right.equalToSuperview().offset(-hSpace)
        }
        
        priceLB.snp.makeConstraints { (make) in
            make.left.equalTo(titleLB)
            make.top.equalTo(titleLB.snp.bottom).offset(8)
        }
        
        numberLB.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-hSpace)
            make.bottom.equalTo(priceLB)
        }
        
        subProductImgsView.snp.makeConstraints { (make) in
            make.left.equalTo(coverImgView)
            make.right.equalToSuperview().offset(-44.up-hSpace)
            make.top.equalTo(coverImgView.snp.bottom).offset(12)
            make.height.equalTo(44.up)
            make.bottom.equalToSuperview().offset(-vSpace)
        }
        
        collectionBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-hSpace)
            make.top.equalTo(subProductImgsView)
            make.width.height.equalTo(subProductImgsView.snp.height)
        }
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(productInfoViewClick(tap:)))
        productInfoView.addGestureRecognizer(tap)
    }
    
    //MARK: - 刷新数据
    func refreshDatas () {
//        coverImgView.eyee_webImage(with: "url", placeholder: nil)
        
        titleLB.text = "Air Jordan 1 Retro High Off-white And Red 北卡红"
        
        priceLB.text = "¥ 2499  市价 ¥2599"
        priceLB.attributedString("市价 ¥2599", color: UIColor.fc2, font: UIFont.pingfangRegular(size: 9.up)!)
        
        numberLB.text = "货号 CD6578-002"
        
        var subImgViewX: CGFloat = 0
        let subImgViewSpace: CGFloat = 6
        for i in 0 ..< 5 {
            let imgView = UIImageView()
            imgView.isUserInteractionEnabled = true
//            imgView.isMultipleTouchEnabled = true
            imgView.border(UIColor.fc2, width: 1)
            if i != 4 {
                imgView.backgroundColor = UIColor.lightGray
            }else {
                imgView.contentMode = .scaleAspectFit
                imgView.image = UIImage(named: "calendar_more picture")
            }
            subProductImgsView.addSubview(imgView)
            
            imgView.snp.makeConstraints { (make) in
                make.left.equalTo(subImgViewX)
                make.top.bottom.equalToSuperview()
                make.width.equalTo(44.up)
            }
            
            subImgViewX = subImgViewX + 44.up + subImgViewSpace
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(subProductImgViewClick(tap:)))
            tap.view?.tag = 100 + i
            imgView.addGestureRecognizer(tap)
        }
    }
    
    //MARK: - 事件
    //自身点击事件
    @objc func productInfoViewClick(tap: UITapGestureRecognizer) {
        self.delegate?.productInfoViewTapAction(dataModel: "")
    }
    
    @objc func subProductImgViewClick(tap: UITapGestureRecognizer) {
        if let view = tap.view as? UIImageView {
            self.delegate?.subProductImgViewTapAction(imgView: view, index: view.tag - 100)
        }
    }
    
    //收藏按钮点击事件
    @objc func collectionBtnAction(sender: UIButton) {
        self.delegate?.collectionAction(sender: sender, dataModel: "")
    }
}
